clear all
do "C:/Userdata/Shared/Dofiles/DoData/Organizations, political participation/programs.do"

capture log close
log using "C:/Userdata/Shared/Logs/Organizations, political participation/datamanagement_other.text", replace text 


* Folder macros
cd "D:/Data/ProjData/Organizations, political participation/mod/"
global scb "D:/SCB_ConPol/Stata"
global dodata "D:/Data/ProjData/Organizations, political participation/mod/"

* Variable macros
global participation_indicators MA0550 MA0551 MA0552 MA0553 MA0554 MA0560 MA0561 MA0562

global member_firstwave MA0050 MA0060 MA0070 MA0080 MA0600 MA0603 MA0606 MA0624 MA0630 
global member_secondwave MA0903 MA0904 MA0905 MA0906 MA0910 MA0911 MA0912 MA0913 MA0914 MA0915
global member_permanent MA0000  

global active_firstwave MA0054 MA0064 MA0074 MA0084 MA0601 MA0604 MA0607 MA0625 MA0631
global active_secondwave MA0604 MA0683 MA0688 MA0607 MA0054 MA0084 MA0625 MA0064 MA0631 MA0074
global active_permanent MA0004 

global member "memberboende memberimmig memberpension memberparent membersport memberenviron memberhandi memberwoman" 
global active "activeboende activeimmig activepension activeparent activesport activeenviron activehandi activewoman"



// Prefixes
// rp	Recoded political participation
// rm	Recoded membership
// ra   Recoded active organization

* Modify files
foreach y in 1992 1993 2000 2001 {
	use "${scb}/ULF/ulf`y'", clear
	
	if `y' < 2000 {
	differentnamesFirst															/*See separate program dofile*/
	}
	if `y' >= 2000 {
	differentnamesSecond
	}
	
	* Drop variables 
	local keepvars LopNr AterPNr $member $active $participation_indicators $member_firstwave $member_secondwave $member_permanent $active_firstwave $active_secondwave $active_permanent 
	ds *
	local all = r(varlist)
	local dropvars : list all - keepvars
	drop `dropvars'
	
	* Destring all variables
	ds *
	foreach v in `r(varlist)' {
		capture replace `v' = "999" if `v' == "NA"
		destring `v', replace 
	}
	
	* Save temporarily
	gen year = `y'
	save "${dodata}/ulf`y'", replace
}

* Append and erase the temporary files
clear
foreach y in 1992 1993 2000 2001 {
	append using "${dodata}/ulf`y'.dta"
	erase "${dodata}/ulf`y'.dta"
}

* Recode variables
foreach v in $participation_indicators {
	gen rp_`v' = .
	replace rp_`v' = 2 if `v' == 1
	replace rp_`v' = 1 if `v' == 2
	replace rp_`v' = 0 if `v' == 3
}

* Generate index of political participation
ds rp_*
egen index = rowtotal(`r(varlist)')
foreach v in $participation_indicators {
	replace index = . if rp_`v' == .
}
label var index "Index pol.part"

* Factor analysis
ds rp_*
factor `r(varlist)'
predict rawfactor
egen factor = std(rawfactor)

* Recode old and permanent membership variables
foreach v in $member_firstwave $member_permanent {
	gen rm_`v' = .
	replace rm_`v' = 1 if `v' == 1
	replace rm_`v' = 0 if `v' == 2
} 

* Recode old and permanent active participation variables
foreach v in $active_firstwave $active_permanent {
	gen ra_`v' = .
	replace ra_`v' = 1 if `v' == 1
	replace ra_`v' = 0 if `v' == 2 | `v' == 0									/*Those who are not members cannot be active members*/
} 

* Recode new membership variables
foreach v in $member_secondwave {
	gen rm_`v' = .
	replace rm_`v' = 1 if inrange(`v',1,8)
	replace rm_`v' = 0 if `v' == 999
} 

* Recode new active participation variables
foreach v in $active_secondwave {
capture confirm variable ra_`v'
if _rc {
	gen ra_`v' = .
	replace ra_`v' = 1 if `v' == 1
	replace ra_`v' = 0 if `v' != 1														
}
} 


* Count types of memberships
ds rm_*
egen Nmemberships = rowtotal(`r(varlist)')


* Count types of active participation organizations
ds ra_*
egen Nactive = rowtotal(`r(varlist)')

* Member in any organisation
gen memberany = .
replace memberany = 1 if Nmemberships > 0 & Nmemberships != .
replace memberany = 0 if Nmemberships == 0

* Active in any organisation
gen activeany = .
replace activeany = 1 if Nactive > 0 & Nactive != .
replace activeany = 0 if Nactive == 0

* Adding Lisa data for covariates
rename year ulfyear
tostring LopNr ulfyear, replace
merge 1:1 LopNr ulfyear using "LISA_panel"										
drop if _merge == 2
drop _merge

* Adding place of residence and inhabitants per km2
tostring ulfyear, replace
merge 1:1 LopNr ulfyear using "kommunkod_panel.dta", nogen keep(1 3)
merge m:1 Kommunkod ulfyear using "invkm2.dta", nogen keep(1 3)


*Replacing active membership with 0 if missing (because you have to be passive member)*
ds active*
foreach k in `r(varlist)' {
	replace `k' = 0 if `k' == . 
}

*Standardize income for each year in the survey

destring LoneInk, replace
egen mean_LoneInk = mean(LoneInk), by(ulfyear)
egen sd_LoneInk = sd(LoneInk), by(ulfyear)
gen zLoneInk = (LoneInk - mean_LoneInk) / sd_LoneInk
drop mean_LoneInk sd_LoneInk

* Adding data from Födelseregistret for addional covariates

merge m:1 LopNr using "Fodelseuppg.dta"
drop if _merge == 2
drop _merge

gen birthyear = real(substr(string(FodArMan),1,4))
destring ulfyear, replace
gen age = ulfyear - birthyear

gen gender = .
replace gender = 0 if Kon == 1
replace gender = 1 if Kon == 2

gen immig = 0
replace immig = 1 if F_delselandGrupp != "Sverige"

destring sysselsatt socialbidragfamilj yearseducation, replace

**Generate period variable for panel analysis*
gen period = 1 if ulfyear < 2000
replace period = 2 if ulfyear >=2000

duplicates tag LopNr, gen(d)
keep if d == 1



*Label*
label var rp_MA0550 "Contact MuniOf"
label var rp_MA0551 "Writing Comment local"
label var rp_MA0552 "Signed petition local"
label var rp_MA0553 "Demonstration/Protest local"
label var rp_MA0554 "Other local"
label var rp_MA0560 "Writing comment"
label var rp_MA0561 "Signed petition"
label var rp_MA0562 "Demonstration/Protest"


label var memberany "Member in any org."
label var activeany "Active in any org."
label var Nmembership "Member in \# of org."
label var Nactive "Active in \# of org."


label var memberboende "Member Housing"
label var memberimmig "Member Immigration"
label var memberpension "Member Pension"
label var memberparent "Member Parent"
label var membersport "Member Sport"
label var memberenviron "Member Environmental"
label var memberhandi "Member Handicap" 	
label var memberwoman "Member WomenOrg"

label var activeboende "Active Housing"
label var activeimmig "Active Immigration"
label var activepension "Active Pension"
label var activeparent "Active Parent"
label var activesport "Active Sport"
label var activeenviron "Active Environmental"
label var activehandi "Active Handicap" 	
label var activewoman "Active WomenOrg"

drop d ra* rm* MA0550 MA0551 MA0552 MA0553 MA0554 MA0560 MA0561 MA0562 UtlSvBakG FodArMan birthyear SyssStatG Sun2000niva_old F_delselandGrupp UtlSvBakG FodArMan MA0000 MA0004 MA0050 MA0054 MA0060 MA0064 MA0070 MA0074 MA0080 MA0084 MA0600 MA0601 MA0603 MA0604 MA0606 MA0607 MA0624 MA0625 MA0630 MA0631 AterPNr MA0683 MA0688 MA0903 MA0904 MA0905 MA0906 MA0911 MA0910 MA0912 MA0913 MA0914 MA0915


save "dataanalysis_otherparticipation.dta", replace

**************************
clear all
capture log close



